<?php

namespace app\api\controller;

use app\api\BaseController;
use app\model\TaskBackImgModel;
use app\model\TaskModel;
use think\facade\Config;
use think\facade\Filesystem;

class Task extends BaseController
{
    public function index()
    {
        $userId = $this->request->userInfo['userId'];

        $list = TaskModel::where('user_id', $userId)
            ->order([

            ])->select();

        return show($list);
    }

    public function changeStatus()
    {
        $params = $this->request->param();

        $taskModel = TaskModel::find($params['id']);

        if ($taskModel->status === 1) {
            $taskModel->status = 2; // 更改为处理中
            $taskModel->save();
        }


        return show();
    }

    public function details()
    {
        $params = $this->request->param();

        $userId = $this->request->userInfo['userId'];

        $taskModel = TaskModel::with(['sendImg','backImg'])
            ->where([
                'user_id' => $userId,
                'id' => $params['id']
            ])->find();

        return show($taskModel);

    }


    public function backData()
    {
        $params = $this->request->param();
        if (empty($params['files'])) {
            return show([],'1','缺少图片');
        }
        $taskModel = TaskModel::find($params['id']);
        $taskModel->back_content = $params['backContent'];
        $taskModel->status = 3; // 进入待审核状态
        $taskModel->save();
        return show();
    }

    public function untreated()
    {
        $userId = $this->request->userInfo['userId'];
        $count = TaskModel::where([
            'user_id' => $userId,
            'status' => 1
        ])->count();

        return show(['taskNum' => $count]);

    }
}